package com.hnepri.fang.ftx2.fetch.residential;


import com.hnepri.fang.ftx2.fetch.APageBridge;
import com.hnepri.util.FileUtil;

import java.io.IOException;

/**
 * 线程对象分区解析
 */
public class ResidentialThread extends Thread{
    private String urlPattern;
    private String city;
    private String path;
    private int startPage;
    private int endPage;
    private APageBridge residentialPageConcrete;


    public String getUrlPattern() {
        return urlPattern;
    }

    public void setUrlPattern(String urlPattern) {
        this.urlPattern = urlPattern;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getStartPage() {
        return startPage;
    }

    public void setStartPage(int startPage) {
        this.startPage = startPage;
    }

    public int getEndPage() {
        return endPage;
    }

    public void setEndPage(int endPage) {
        this.endPage = endPage;
    }

    public APageBridge getResidentialPageConcrete() {
        return residentialPageConcrete;
    }

    public void setResidentialPageConcrete(APageBridge residentialPageConcrete) {
        this.residentialPageConcrete = residentialPageConcrete;
    }

    @Override
    public void run() {
        for( int i = startPage; i <= endPage; i++) {
            String url = urlPattern.replace("${city}", city).replace("${page}", "1");
            try {
                String info = residentialPageConcrete.getPageInfo(url);
                String fpath = path + "/" + i + ".txt";
                FileUtil.writeFile(fpath, info);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
